// pages/chartlook/chartlook.js
var imageUtil = require('../../utils/util.js');
import { clockList } from '../data/clock.data.js';
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    clockList:clockList,
    dietId:'',
    type:'',
    time:'',
    data:''
  },
  // 转发
  onShareAppMessage: function () {
    return {
      title: '爱丁生活方式管理营',
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    app.getUserInfo(function (userInfo) {
      that.setData({ userInfo: userInfo })
    })

    
    that.setData({
      type: options.type,
      dietId: options.dietId,
      time: options.time,
      sign:options.sign,
      times:options.times
    })


    //改变导航头的名称
    if (this.data.type == 11 & this.data.times == 1) {
      wx.setNavigationBarTitle({
        title: '早间运动'
      })
    }
    if (this.data.type == 11 & this.data.times == 2) {
      wx.setNavigationBarTitle({
        title: '上午运动'
      })
    }
    if (this.data.type == 11 & this.data.times == 4) {
      wx.setNavigationBarTitle({
        title: '下午运动'
      })
    }
    if (this.data.type == 11 & this.data.times == 6) {
      wx.setNavigationBarTitle({
        title: '夜间运动'
      })
    }

    //改变导航头的名称
    if (this.data.type == 7 & this.data. times == 1) {
      wx.setNavigationBarTitle({
        title: '体温打卡'
      })
    }


    
if(that.data.type==7){
  // 请求接口，渲染页面
  var url = '/IbabyWebService/camp/dietHistoryToday?patientid=' + wx.getStorageSync('userInfo').patientid + '&type=' + that.data.type;
  app.https.get(that, url, {}, function (res) {
    try {
      var last = {};
      last.temperature = JSON.parse(res[0].detail).temperature;
      last.time = JSON.parse(res[0].detail).starttime;
      // console.log(last.time)
      var endtime = last.time.split(" ");
      // console.log(endtime);
      last.timestar = endtime[1];//拿到日期时间中的时间 例如：08：00
      // console.log(timestar);
      last.datestar = endtime[0];//日期

      last.createtime = res[0].createtime;
      that.setData({
        'last': last,
      });
    } catch (err) {
      console.log(err)
    }
  })
}else if(that.data.type==11){
  //运动渲染
  // 请求接口，渲染页面
  var url = '/IbabyWebService/camp/dietHistoryToday?patientid=' + wx.getStorageSync('userInfo').patientid + '&type=' + that.data.type;
  app.https.get(that, url, {}, function (res) {
    console.log(res)
    var list=res;
    if(that.data.times==1){
      for(var i=0;i<list.length;i++){
        if(list[i].times==1){
          try {
            var last = {};
            //无法带值，因为无法固定值的选项，运动打卡是多选，
            last.motiontime = JSON.parse(list[i].detail).starttime
            last.createtime = list[i].createtime;
            that.setData({
              'last': last,
            });
          } catch (err) {
            console.log(err)
          }
        }
      }
      
    }else if(that.data.times==2){
      for (var i = 0; i < list.length; i++) {
        if (list[i].times == 2) {
          try {
            var last = {};
            //无法带值，因为无法固定值的选项，运动打卡是多选，
            last.motiontime = JSON.parse(list[i].detail).starttime
            last.createtime = list[i].createtime;
            that.setData({
              'last': last,
            });
          } catch (err) {
            console.log(err)
          }
        }
      }

    }else if(that.data.times==4){
      for (var i = 0; i < list.length; i++) {
        if (list[i].times == 4) {
          try {
            var last = {};
            //无法带值，因为无法固定值的选项，运动打卡是多选，
            last.motiontime = JSON.parse(list[i].detail).starttime
            last.createtime = list[i].createtime;
            that.setData({
              'last': last,
            });
          } catch (err) {
            console.log(err)
          }
        }
      }

    }else if(that.data.times==6){
      for (var i = 0; i < list.length; i++) {
        if (list[i].times == 6) {
          try {
            var last = {};
            //无法带值，因为无法固定值的选项，运动打卡是多选，
            last.motiontime = JSON.parse(list[i].detail).starttime
            last.createtime = list[i].createtime;
            that.setData({
              'last': last,
            });
          } catch (err) {
            console.log(err)
          }
        }
      }
    }
   
    
  })
}

  },

  //跳转首页
  returnbutton: function () {
    wx.switchTab({
      url: '../index/index',//跳转到首页页面tab
    })
  },


// 点击修改
  subimtmodify:function(){

    if(this.data.type==7){
      wx.redirectTo({
        url: '../temperatureone/temperatureone?id=7&type=' + this.data.type + "&sign=" +this.data.sign+"&times="+this.data.times +'&dietId=' + this.data.dietId + '&temperature=' + this.data.last.temperature + "&startdate=" + this.data.last.datestar + '&timestar=' + this.data.last.timestar
      })
    }else if(this.data.type==11){
      wx.redirectTo({
        url: '../temperatureone/temperatureone?id=11&type=' + this.data.type + "&sign=" + this.data.sign + "&times=" + this.data.times+ '&dietId=' + this.data.dietId +'&motiontime=' + this.data.last.motiontime
      })
    }
   
  },

// 点击跳转图表页面
  lookcharttap:function(){
    wx.redirectTo({
      url: "../chart/chart?id=" + this.data.dietId + '&type=' + this.data.type + "&sign=" + this.data.sign + "&times=" + this.data.times
    })
  },



  
})


